varying vec2 vUv;
uniform sampler2D tex;
uniform sampler2D tex2;
uniform float _rot;
uniform float dispFactor;
uniform float effectFactor;

float rand(vec2 n) {
    return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}

float noise(vec2 p){
    vec2 ip = floor(p);
    vec2 u = fract(p);
    u = u*u*(3.0 - 2.0 * u);

    float res = mix(
    mix(rand(ip), rand(ip + vec2(1.0, 0.0)), u.x),
    mix(rand(ip + vec2(0.0, 1.0)), rand(ip + vec2(1.0, 1.0)), u.x), u.y);
    return res*res;
}

void main() {
    vec2 uv = vUv;
//    float noiseFactor = noise(gl_FragColor.rg);
    float noiseFactor = noise(gl_FragCoord.rg * 0.4);
    vec2 distortedPosition = vec2(uv.x + dispFactor * noiseFactor, uv.y);
    vec2 distortedPosition2 = vec2(uv.x, uv.y - (1.0 - dispFactor) * noiseFactor);
    vec4 _texture = texture2D(tex, distortedPosition);
    vec4 _texture2 = texture2D(tex2, distortedPosition2);
    vec4 finalTexture = mix(_texture, _texture2, dispFactor);
    gl_FragColor = finalTexture;
    #include <tonemapping_fragment>
    #include <encodings_fragment>
}
